package cn.itsub.sodo.core;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.itsub.sodo.Scene;
import cn.itsub.sodo.core.types.Size;
import cn.itsub.sodo.timer.HiScheduler;
import cn.itsub.sodo.view.HiView;
import cn.itsub.sodo.run.Application;

/**
 * 导演
 * @author Mr.Xia@zhenzhigu.com
 * @Date 2016年10月26日
 */
public class Director {
	public static Date initDate=new Date();					//程序启动时间	
	public static Director director=null;					//全局导演
	public static boolean displayStats=false;				//是否显示引擎状态
	
	public static Size size=new Size(800,600);				//窗口尺寸
	public static Scene runningScene = null;				//当前的场景
	public static Scene nextScene = null;
	public static HiView view = null;
	
	public static long lastUpdate=0;	//上一次刷新的时间
	public static float dt=0.0f;		//两次刷新的间隔
	
	public float _animationInterval = 1.0f/60;			//动画间隔
	public float _oldAnimationInterval = 0;
	public HiScheduler scheduler = HiScheduler.getInstance();
	
	public static boolean isPaused = false;	//暂停
	public boolean _invalid = true;			//动画无效
	
	/* Display FPS */
	public boolean displayFPS = Config.DEFAULT_SHOW_FPS;
    private static int frames_;
    private static float accumDt_;
    public static float frameRate_;
    

	private Director() {
		
	}
	public static Director getInstance(){
		if(director==null){
			director = new Director();
		}
		return director;
	}
	public void setView(HiView view){
		this.view = view;
	}
	public HiView getView(){
		return this.view;
	}
	public void runScene(Scene scene){
		runningScene = scene;
		startAnimation();
	}
	/* ----- Pause and Resume ----- */
	public void pause()
	{
	    if (isPaused)
	    {
	        return;
	    }
	    _oldAnimationInterval = _animationInterval;
	    // when paused, don't consume CPU
	    setAnimationInterval(1 / 4.0f);
	    isPaused = true;
	}

	public void resume()
	{
	    if (! isPaused)
	    {
	        return;
	    }
	    setAnimationInterval(_oldAnimationInterval);
	    isPaused = false;
	}
	/* ----- Start/Stop For Animation  ----- */
	public void startAnimation()
	{
	    _invalid = false;
	    Application.getInstance().setAnimationInterval(_animationInterval);
	}
	public void stopAnimation(){
		_invalid = true;
	}
	public void setDisplayStats(boolean display){
		this.displayFPS = display;
	}
	public void setAnimationInterval(float interval){
		this._animationInterval = interval;
		if (! _invalid)
	    {
	        stopAnimation();
	        startAnimation();
	    }  
	}
	public void mainLoop(){
		if (!_invalid){
			calculateDeltaTime();
			if(!isPaused) {
	        	HiScheduler.getInstance().tick(dt);
	        }
	        this.view.repaint();
	    }
	}
	
	private static void calculateDeltaTime() {
        long now = System.currentTimeMillis();
        dt = (now - lastUpdate) * 0.001f;
        dt = Math.max(0, dt);
        lastUpdate = now;
    }
	public static float getFPS() {

		frames_++;
        accumDt_ += dt;

        if (accumDt_ > Config.DIRECTOR_FPS_INTERVAL) {
            frameRate_ = frames_ / accumDt_;
            frames_ = 0;
            accumDt_ = 0;
        }
        return frameRate_;
        
    }
}
